import random
import time
import tkinter
import pygame


pygame.mixer.init()
root=tkinter.Tk()
root.title('课堂随机点名系统')
root.resizable(False,False)
title=tkinter.Label(root,text='欢迎使用课堂随机点名系统',font=("宋体",25),bg='white')
title.pack(pady=20)
root.config(bg='white')



def center_window(root, width, height):
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size) 
center_window(root,800,330)

def chushihua():
    global names
    global select
    names=['卫新宇','王尚熙','宁  钰','黄天龙','张奥玮','刘思如','王京晶','林怡彤','时培杰','杨柳炜','袁紫涵','张卓琳','孙艺丹','郭泽坤','樊思晨','马梓航','崔梦垚','林钲皓','高梓萌','李嘉图','靳昊润','张巧巧','董晓泰','商妍茹','毕昭阳','胡川琪','吕承玺','张颢译','景元瑞','柴嘉晨','梁靖怡','许薰元','郭亚鑫','程相杰','尤常凯','王浩宇','马佳敏','李金阳','宋文晖','李季泽','弓士森','杨子轩','上官佐','胡梦涛','宁一张','宁一弛','张宸坤']
    select=[]

chushihua()   

name=tkinter.StringVar()
name.set('请选择人数')
num=0
key=0
t=0
sound1=pygame.mixer.Sound('滚动.wav')
sound2=pygame.mixer.Sound('完成.wav')

def startname():
        global i
        global name
        global names
        global t
        global selectlist
        global suiji
        global select
        global huifu
        global key

        i=0
        t=0
        start_time = time.time()
        starttime = time.time()
        select=[]
        namedel()
        jian.config(state='disabled')
        jia.config(state='disabled')
        start.config(state='disabled')
        repoint.config(state='disabled')
        window.config(state='disabled')
        
        if len(names)<num:
             selectlist.pack_forget()
             scrollbar.pack_forget()
             suiji.pack(side=tkinter.TOP,padx=150,pady=9)
             name.set('名单中剩余人数不足！')
             if key==0:
                huifu=tkinter.Button(mingzi,font=("宋体",20,'bold'),relief=tkinter.FLAT,bg='white',text='点击恢复名单',command=recovery)
                huifu.pack(side=tkinter.TOP)
                key=1
             if num>=1:
                jian.config(state='normal')
             if num<=47:
                jia.config(state='normal')
             
             repoint.config(state='normal')
             window.config(state='normal')
             return
        if num==0:
                selectlist.pack_forget()
                scrollbar.pack_forget()
                suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
                name.set('请选择人数')
        if window_var.get()==0:
            if num>1:
                suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
                selectlist.pack(side=tkinter.RIGHT)
                selectlist.config(width=24, height=4)
                scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y,before=selectlist)
                selectlist.config(yscrollcommand=scrollbar.set)
            while num!=0:
                sound1.play()
                root.after(190, root.update())
                name.set(random.choice(names))
                end_time = time.time()
                endtime = time.time()
                if end_time-start_time>random.uniform(0,3) and endtime-starttime>1.5 and num!=1:
                    selectlist.config(state=tkinter.NORMAL)
                    if t!=0:
                        selectlist.tag_configure("center", justify='center')
                        selectlist.insert(tkinter.END,'，')
                    selectlist.tag_configure("center", justify='center')
                    selectlist.insert(tkinter.END,name.get())
                    names.remove(name.get())
                    t+=1
                    selectlist.config(state=tkinter.DISABLED)
                    i+=1
                    start_time = time.time()
                    if i==num:
                        break
                if num==1:
                    selectlist.pack_forget()
                    scrollbar.pack_forget()
                    suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
                    if endtime-starttime>3:
                        names.remove(name.get())
                        break
                root.update()
            sound2.play()
        if window_var.get()==1:
            selectlist.pack_forget()
            scrollbar.pack_forget()
            while num!=0:
                sound1.play()
                root.after(190, root.update())
                name.set(random.choice(names))
                endtime = time.time()
                if i!=num:
                    select.append(name.get())
                    names.remove(name.get())
                    t+=1
                    i+=1
                if endtime-starttime>3:
                    break
                root.update()
            jian.config(state='normal')
            jia.config(state='normal')
            start.config(state='normal')
            repoint.config(state='normal')
            window.config(state='normal')
            if num!=0 and num>1:
                name.set('请点击“开始”进行点名')
                newwindow()
            elif num==0:
                name.set('请选择人数')
            else:
                name.set('，'.join(select))
        jian.config(state='normal')
        jia.config(state='normal')
        start.config(state='normal')
        repoint.config(state='normal')
        window.config(state='normal')
        
def newwindow():
    global new_root
    new_root = tkinter.Tk()
    new_root.title('名单')
    new_root.config()
    new_root.resizable(False,False)

    def center_window(new_root, width, height):
        screenwidth = new_root.winfo_screenwidth()
        screenheight = new_root.winfo_screenheight()
        size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        new_root.geometry(size) 
    
    if len(select)==2:
        sound2.play()
        center_window(new_root,265,40)
    elif len(select)==3:
        sound2.play()
        center_window(new_root,400,40)
    elif len(select)==4:
        sound2.play()
        center_window(new_root,540,40)
    elif len(select)==5:
        sound2.play()
        center_window(new_root,670,40)
    else:
        sound2.play()
        if len(select)%5!=0:
            center_window(new_root,710,(len(select)//5)*35+35)
        else:
            center_window(new_root,710,(len(select)//5)*35)

    text='，'.join(select)
    selectlist=tkinter.Text(new_root,font=("宋体",25,'bold'),relief=tkinter.FLAT,bg='white')
    selectlist.pack(expand=True)
    scrollbar=tkinter.Scrollbar(new_root,command=selectlist.yview,bg='white')
    scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y,before=selectlist)
    selectlist.config(yscrollcommand=scrollbar.set)

    selectlist.insert(tkinter.END,text)
    selectlist.config(state=tkinter.DISABLED)

    new_root.mainloop()

def namedel():
        global selectlist
        global t
        global names
        global suiji
        global repoint_var
        root.after_cancel
        if repoint_var.get()==1:
            chushihua()
        selectlist.config(state=tkinter.NORMAL)
        t=0
        selectlist.delete(0.1,tkinter.END)
        selectlist.config(state=tkinter.DISABLED)
     
def recovery():
        global key
        chushihua()
        suiji.pack_forget()
        huifu.pack_forget()
        key=0
        suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
        if num!=0 and num>1:
            name.set('请点击“开始”进行点名')
        if num==0:
            name.set('请选择人数')
        start.config(state='normal')

def gettime():
      timestr = time.strftime("%H:%M:%S")
      lb.configure(text=timestr)
      root.after(1000,gettime)

def numplus():
        global num
        global geshu
        global name
        global selectlist
        global scrollbar
        selectlist.pack_forget()
        scrollbar.pack_forget()
        if num<47:
            num+=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jian.config(state=tkinter.NORMAL)
        if num==47:
             jia.config(state=tkinter.DISABLED)
        if num!=0:
            name.set('请点击“开始”进行点名')

def numminus():
        global num
        global geshu
        global jian
        global jia
        global selectlist
        global scrollbar
        selectlist.pack_forget()
        scrollbar.pack_forget()
        if num>0:
            num-=1
            geshu.destroy()
            geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
            geshu.pack(side=tkinter.LEFT)
            jia.config(state=tkinter.NORMAL)
        if num==0:
            jian.config(state=tkinter.DISABLED)
            name.set('请选择人数')
        if num!=0:
            name.set('请点击“开始”进行点名')


mingzi=tkinter.Frame(root,bg='white')
suiji=tkinter.Label(mingzi,textvariable=name,font=("宋体",30,'bold'),relief=tkinter.FLAT,bg='white')
suiji.pack(side=tkinter.LEFT,padx=150,pady=33)
selectlist=tkinter.Text(mingzi,font=("宋体",20,'bold'),relief=tkinter.FLAT,width=24, height=4,state=tkinter.DISABLED,bg='white')
scrollbar=tkinter.Scrollbar(mingzi,command=selectlist.yview,bg='white')
mingzi.pack(pady=20)


fuxuan=tkinter.Frame(root,bg='white')
repoint_var=tkinter.IntVar()
repoint=tkinter.Checkbutton(fuxuan,text='重复点名',variable=repoint_var,font=("宋体",15,'bold'),bg='white')
window_var=tkinter.IntVar()
window=tkinter.Checkbutton(fuxuan,text='在新窗口显示名单',variable=window_var,font=("宋体",15,'bold'),bg='white')
kong=tkinter.Label(fuxuan,bg='white')
fuxuan.pack(side=tkinter.TOP)
repoint.pack(side=tkinter.LEFT)
kong.pack(side=tkinter.LEFT,padx=230)
window.pack(side=tkinter.LEFT)


anniu=tkinter.Frame(root,bg='white')

renshu=tkinter.Label(anniu,text='人数:',font=("宋体",20,'bold'),bg='white')
renshu.pack(side=tkinter.LEFT,padx=50)

jian=tkinter.Button(anniu,text='-',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numminus,state=tkinter.DISABLED)
jian.pack(side=tkinter.LEFT)

geshu=tkinter.Label(anniu,text=num,font=("宋体",20,'bold'),width=5,bg='white')
geshu.pack(side=tkinter.LEFT)

jia=tkinter.Button(anniu,text='+',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=numplus)
jia.pack(side=tkinter.RIGHT)

start=tkinter.Button(anniu,text='开始',relief=tkinter.FLAT,font=("宋体",20,'bold'),bg='white',command=startname)
start.pack(side=tkinter.RIGHT,padx=50,before=jia)
anniu.pack()


lb = tkinter.Label(root,text='',font=("宋体",15),bg='white')
lb.pack(side=tkinter.LEFT)
gettime()


shuiyin=tkinter.Label(root,text='Fengchunye',font=("宋体",15),bg='white')
shuiyin.pack(side=tkinter.RIGHT)

root.mainloop()